package com.sunriz.refreshhcdata.refresh;

import com.sunriz.refreshhcdata.POJO.InfoWriteDO;
import com.sunriz.refreshhcdata.POJO.InfoWriteDTO;
import com.sunriz.refreshhcdata.POJO.McHcRelationshipDO;
import com.sunriz.refreshhcdata.mapper.InfoWriteMapper;
import com.sunriz.refreshhcdata.mapper.McHcRelationshipMapper;
import com.sunriz.refreshhcdata.utils.NumericalConversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.Month;
import java.util.*;

/**
 * ClassName: Refresh
 * Package: com.sunriz.refreshhcdata.refresh
 * Description:
 *
 * @Author BuTang
 * @Create 2024/9/23 14:24
 * @Version 1.0
 */
@Component
public class Refresh {
    @Autowired
    com.sunriz.refreshhcdata.mapper.RefreshMapper refresh;

    @Autowired
    InfoWriteMapper infoWriteMapper;

    @Autowired
    McHcRelationshipMapper relationshipMapper;

//    @Scheduled( fixedRate = 1000L )
    public void setRefresh(){
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        int hour = now.getHour();
        int minute = now.getMinute();
        int second = now.getSecond();

        refresh.setTime(year,month,day,hour,minute,second);
    }

    /**
     * 10s刷新上位机的数据
     * 用于下发
     */
        @Scheduled( fixedRate = 10000L )
        public void updateDataForDevice(){
            Random random = new Random();

            // 从数据库获取配置，创建返回集合
            List<InfoWriteDO> list = infoWriteMapper.getList();
            List<InfoWriteDTO> returnList = new ArrayList<>();

            List<McHcRelationshipDO> allInfo = relationshipMapper.getAllInfo();

            // 将关系表转换为key-value的形式
            Map<String,McHcRelationshipDO> relationMap = new HashMap();
            allInfo.forEach(mcHcRelationshipDO -> {
                String hcPropertyKey = mcHcRelationshipDO.getHcPropertyKey();
                relationMap.put(hcPropertyKey,mcHcRelationshipDO);
            });

            for (int i = 0; i < list.size(); i++) {
                InfoWriteDO infoWriteDO = list.get(i);

                int id = infoWriteDO.getId();
                double minValue = infoWriteDO.getMinValue();
                double maxValue = infoWriteDO.getMaxValue();
                String hcPropertyKey = infoWriteDO.getHcPropertyKey();


                long randomValue = (long) (minValue + (maxValue - minValue) * random.nextDouble());

                // 进行精度转换
                McHcRelationshipDO relation = relationMap.get(hcPropertyKey);
                int byteSize = relation.getByteSize();
                boolean needSignConversion = relation.needSignConversion;
                double dataPrecision = relation.getDataPrecision();
                String s = NumericalConversion.sendConversion(randomValue, byteSize, needSignConversion, dataPrecision);

                infoWriteMapper.setValue(id,s);
            }
        }

}
